// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   register_draft_ok.java

package manufacture.apply;

import include.nseer_cookie.*;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;

public class register_draft_ok extends HttpServlet
{

	public register_draft_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		nseer_db_backup1 manufacture_db = new nseer_db_backup1(dbApplication);
		nseer_db_backup1 stock_db = new nseer_db_backup1(dbApplication);
		nseer_db_backup1 crm_db = new nseer_db_backup1(dbApplication);
		counter count = new counter(dbApplication);
		try
		{
			if (manufacture_db.conn((String)dbSession.getAttribute("unit_db_name")) && stock_db.conn((String)dbSession.getAttribute("unit_db_name")) && crm_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String product_ID[] = request.getParameterValues("product_ID");
				String product_name[] = request.getParameterValues("product_name");
				String product_describe[] = request.getParameterValues("product_describe");
				String type[] = request.getParameterValues("type");
				String amount[] = request.getParameterValues("amount");
				String pay_ID_group[] = request.getParameterValues("pay_ID_group");
				String designer = request.getParameter("designer");
				String register = request.getParameter("register");
				String register_time = request.getParameter("register_time");
				String bodyb = new String(request.getParameter("remark").getBytes("UTF-8"), "UTF-8");
				String remark = exchange.toHtml(bodyb);
				String time = "";
				Date now = new Date();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
				time = formatter.format(now);
				int n = 0;
				int m = 0;
				for (int i = 0; i < product_ID.length; i++)
				{
					for (StringTokenizer tokenTO1 = new StringTokenizer(pay_ID_group[i], ", "); tokenTO1.hasMoreTokens();)
					{
						String sql5 = (new StringBuilder("select * from stock_pay where manufacture_apply_tag='1' and pay_ID='")).append(tokenTO1.nextToken()).append("'").toString();
						ResultSet rs5 = stock_db.executeQuery(sql5);
						if (rs5.next())
							n++;
					}

					if (Double.parseDouble(amount[i]) != 0.0D)
						m++;
				}

				if (n == 0)
				{
					if (m == 0)
					{
						for (int i = 0; i < product_ID.length; i++)
						{
							for (StringTokenizer tokenTO = new StringTokenizer(pay_ID_group[i], ", "); tokenTO.hasMoreTokens();)
							{
								String sql2 = (new StringBuilder("select * from stock_pay where pay_ID='")).append(tokenTO.nextToken()).append("'").toString();
								ResultSet rs2 = stock_db.executeQuery(sql2);
								if (rs2.next())
								{
									String sql3 = (new StringBuilder("update crm_order set manufacture_tag='2' where order_ID='")).append(rs2.getString("reasonexact")).append("'").toString();
									crm_db.executeUpdate(sql3);
									String sql4 = (new StringBuilder("update stock_pay set manufacture_apply_tag='1' where pay_ID='")).append(rs2.getString("pay_ID")).append("'").toString();
									stock_db.executeUpdate(sql4);
								}
							}

						}

					} else
					{
						String apply_ID = NseerId.getId(getClass().getResource("").toString(), (String)dbSession.getAttribute("unit_db_name"));
						for (int i = 0; i < product_ID.length; i++)
							if (Double.parseDouble(amount[i]) != 0.0D)
							{
								for (StringTokenizer tokenTO = new StringTokenizer(pay_ID_group[i], ", "); tokenTO.hasMoreTokens();)
								{
									String sql2 = (new StringBuilder("select * from stock_pay where pay_ID='")).append(tokenTO.nextToken()).append("'").toString();
									ResultSet rs2 = stock_db.executeQuery(sql2);
									if (rs2.next())
									{
										String sql3 = (new StringBuilder("update crm_order set manufacture_tag='2' where order_ID='")).append(rs2.getString("reasonexact")).append("'").toString();
										crm_db.executeUpdate(sql3);
										String sql4 = (new StringBuilder("update stock_pay set manufacture_apply_tag='1' where pay_ID='")).append(rs2.getString("pay_ID")).append("'").toString();
										stock_db.executeUpdate(sql4);
									}
								}

							}

						stock_db.commit();
						for (int i = 0; i < product_ID.length; i++)
							if (Double.parseDouble(amount[i]) != 0.0D)
							{
								String sql = (new StringBuilder("insert into manufacture_apply(apply_ID,product_ID,product_name,product_describe,type,amount,pay_ID_group,designer,remark,register,register_time,check_tag) values('")).append(apply_ID).append("','").append(product_ID[i]).append("','").append(product_name[i]).append("','").append(product_describe[i]).append("','").append(type[i]).append("','").append(amount[i]).append("','").append(pay_ID_group[i]).append("','").append(designer).append("','").append(remark).append("','").append(register).append("','").append(register_time).append("','5')").toString();
								manufacture_db.executeUpdate(sql);
							}

					}
					String sql3 = "update manufacture_apply set check_tag='5' where product_ID='product_ID'";
					manufacture_db.executeUpdate(sql3);
					response.sendRedirect("manufacture/apply/register_draft_ok.jsp?finished_tag=0");
				} else
				{
					response.sendRedirect("manufacture/apply/register_draft_ok.jsp?finished_tag=1");
				}
				manufacture_db.commit();
				crm_db.commit();
				crm_db.close();
				stock_db.close();
				manufacture_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
